﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Cinch;
using SLS.Controls;
using SLS.ExClassLib.Event;
using SLS.ExClassLib.Events;
using SLS.Base;
namespace SLS.Document
{
    public partial class DocumentManagement
    {
        #region===============SINGLETON===============
        /// <summary>
        /// Singleton
        /// </summary>
        public static readonly DocumentManagement Instance = new DocumentManagement();
        public Dictionary<string, object> SetUpdoc = new Dictionary<string, object>();
        public string ClientWFManagerAddress = "";
        public ArrayList DocumentList
        {
            get;
            set;
        } 
        /// <summary>
        /// Private constructor for singleton pattern
        /// </summary>
        private DocumentManagement()
        {
            
        }

        #endregion

        #region =================PRIVATE=================

        public ExTabControl CenterTab = null;
        public ExTabControl ServiceTab = null;

        #endregion

        #region===============PROPERTIES===============
        public ViewModelBase CenterDocuments;
        public ViewModelBase WFEditDocuments; 
        public SLBC ActiveDocument
        {
            get { return (SLBC)(CenterTab.SelectedItem as TabItem).Content; }
        }
        public SLBC ActiveSLBC
        {
            get
            {
                return GetParentEx(m_ActiveItemsControl);
            }
        }
        public DataTable ActiveDataTable
        {
            get
            {
                if (m_ActiveItemsControl != null)
                {
                    return (DataTable)m_ActiveItemsControl.ItemsSource;
                }
                else
                {
                    return null;
                }
            }
        }
        private SLBC GetParentEx(ItemsControl ThisActiveItemsControl)
        {
            DependencyObject parent = LogicalTreeHelper.GetParent(ThisActiveItemsControl);

            while (parent != null &&
                (!(parent is SLBC)))
                parent = LogicalTreeHelper.GetParent(parent);

            return parent as SLBC;
        }
        private ItemsControl m_ActiveItemsControl;
        public ItemsControl ActiveItemsControl
        {
            get
            {
                return m_ActiveItemsControl;
            }
            set
            {
                if (m_ActiveItemsControl == null || m_ActiveItemsControl != value)
                {
                    m_ActiveItemsControl = value;
                    EventDispatcher.Instance.NotifyPropertyChanged("ActiveItemsControl");
                }
            }
        }

        #endregion

   

        #region===============INTERNALS===============
        public SLBC OpenDocument(string doctype, string masterAddress, string Container, string typecontainer = "ExTabControl", bool single = true)
        {
            /*
             * 1.指明放置位置
             * 2.提供初始化的一些参数
             * 3.加入联邦
             */
            SLBC doc = null;
            object MasterAddress = null;
            ExTabControl sofa = null;
            try
            {
                #region============EXISTS==========================
                MasterAddress = FindDocumentByAddress(masterAddress, "");
                if (MasterAddress == null)
                    return null;
                doc = (SLBC)FindDocumentByAddress("", doctype);
                if (doc != null
                    && single)
                {
                    return doc;
                }
                if (typecontainer == "ExTabControl")
                {
                    if (masterAddress == "MainWindow")
                    {
                        sofa = (ExTabControl)((Window)MasterAddress).FindName(Container);
                    }
                    else
                    {
                        sofa = (ExTabControl)((SLBC)MasterAddress).FindName(Container);
                    }
                }
                #endregion

                #region============CREATE=========================
                Tracer.Verbose("DocumentManagement", " start createDocument " + DateTime.Now.Ticks.ToString() + " " + doctype);
                //doc = CreateDocument(doctype);
                Tracer.Verbose("DocumentManagement", " finish createDocument " + DateTime.Now.Ticks.ToString() + " " + doctype);
                Append(doc, doctype, sofa);
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }
            return doc;
        }
        public void OpenDocument(string doctype, string Address)
        {
            try
            {
                ViewModelBase vwbase = null;
                switch (Address)
                {
                    case "CenterDocuments":
                        vwbase = CenterDocuments;
                        break;
                    case "WFEditDocuments":
                        vwbase = WFEditDocuments;
                        break;
                    default:
                        break;
                } 
                RegisterDocument(doctype);
                WorkspaceData wks = new WorkspaceData("", doctype, null, doctype.ToString(), true);
                vwbase.Documents.Add(wks);
                vwbase.SetActiveWorkspace(wks);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }
        }
        public void SetActiveDocument(string address)
        {
            try
            {

                TabItem acttab = null;
                var query = from TabItem exc in CenterTab.Items
                            where ((SLBC)exc.Content).Address == address
                            select exc;
                foreach (TabItem tab in query)
                {
                    acttab = tab;
                }
                if (acttab != null)
                {
                    acttab.Focus();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public object FindDocumentByAddress(string address, string type)
        {
            object doc = null;
            try
            {
                var query = from DocumentAddress temp in DocumentList
                            where temp.Address == address
                            select temp;
                foreach (DocumentAddress docadd in query)
                {
                    doc = docadd.doc;
                }
                if (doc == null
                    &&
                    type != "")
                {
                    query = from DocumentAddress temp in DocumentList
                            where temp.DocType == type
                            select temp;
                    foreach (DocumentAddress docadd in query)
                    {
                        doc = docadd.doc;
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return doc;
        }
        #endregion
    }
}
